package com.chengdu.third.controller.admin;



import com.chengdu.third.pojo.Csrx;
import com.chengdu.third.pojo.Zwxx;
import com.chengdu.third.service.ICsrxService;
import com.chengdu.third.service.IZwxxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;


@Controller
@RequestMapping("/admin")
public class ConvenientServiceController {

    @Autowired
    private IZwxxService zwxxService;

    @Autowired
    private ICsrxService csrxService;



    //查询所有市级信件
    @GetMapping("/getConList")
    public String getConList(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum, 5);
        List<Zwxx> byRec1 = zwxxService.findByRec1();
        //得到分页结果对象
        PageInfo<Zwxx> pageInfo = new PageInfo<>(byRec1);
        //根据pageInfo来展示出数据
        model.addAttribute("pageInfo", pageInfo);
        return "admin/convenientService_list1";
    }

    //查询所有区级信件
    @GetMapping("/getCon2List")
    public String getCon2List(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum, 5);
        List<Zwxx> byRec1 = zwxxService.findByRec2();
        //得到分页结果对象
        PageInfo<Zwxx> pageInfo = new PageInfo<>(byRec1);
        //根据pageInfo来展示出数据
        model.addAttribute("pageInfo", pageInfo);
        return "admin/convenientService_list2";
    }

    //查询所有乡镇级信件
    @GetMapping("/getCon3List")
    public String getCon3List(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum, 5);
        List<Zwxx> byRec1 = zwxxService.findByRec3();
        //得到分页结果对象
        PageInfo<Zwxx> pageInfo = new PageInfo<>(byRec1);
        //根据pageInfo来展示出数据
        model.addAttribute("pageInfo", pageInfo);
        return "admin/convenientService_list3";
    }


    //查询所有村级信件
    @GetMapping("/getCon4List")
    public String getCon4List(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum, 5);
        List<Zwxx> byRec1 = zwxxService.findByRec4();
        //得到分页结果对象
        PageInfo<Zwxx> pageInfo = new PageInfo<>(byRec1);
        //根据pageInfo来展示出数据
        model.addAttribute("pageInfo", pageInfo);
        return "admin/convenientService_list4";
    }

    //查询所有热线电话记录
    @GetMapping("/getHotLine")
    public String getHotLine(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum, 5);
        List<Csrx> list = csrxService.findAll();
        //得到分页结果对象
        PageInfo<Csrx> pageInfo = new PageInfo<>(list);
        //根据pageInfo来展示出数据
        model.addAttribute("pageInfo", pageInfo);
        return "admin/HotLine";
    }

    //删除热线电话记录
    @GetMapping("/hotLineDelete/{id}/delete")
    public String hotLineDelete(@PathVariable Long id, RedirectAttributes attributes){
        csrxService.deleteByid(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/getHotLine";
    }

    //删除信件信息
    @GetMapping("/ConDelete/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
      zwxxService.deleteByid(id);
        attributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/admin/getConList";
    }

    //查看信件具体内容
    @GetMapping("/ConShow/{id}/show")
    public String show(@PathVariable Long id,Model model){
        Zwxx zwxx = zwxxService.getZwxxById(id);
        model.addAttribute("zwxx",zwxx);
        return "admin/ConService_show";
    }




}
